<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('项目名称')">
            <template>
              <router-link
                target="_blank"
                :to="{path:'/deal/home/dealBaseHome',query:{id:form.dealId,companyId:form.companyId}}"
                class="link_a"
              >{{ form.dealName }} </router-link>
            </template>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('标的公司名称')">
            <template>
              <router-link
                target="_blank"
                :to="{path:'/company/base/companyBaseHome',query:{companyId:form.companyId}}"
                class="link_a"
              >{{ form.companyName }}</router-link>
            </template>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('申请标题')" prop="actTitle">
            <el-input v-model="form.actTitle" placeholder="请输入标题" show-word-limit :maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('项目负责人')" prop="userId">
            <UserSelect v-model="form.userId" :placeholder="$t('项目负责人')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('尽调方式')" prop="ddMode">
            <el-select v-model="form.ddMode" value="" :placeholder="$t('尽调方式')">
              <el-option
                v-for="dict in ddModeOptions"
                :key="dict.dictValue"
                :label="$t(dict.dictLabel)"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item v-if="ddTypeOptions !== null" :label="$t('尽调类型')" prop="ddType">
            <el-checkbox-group v-model="form.ddType">
              <el-checkbox
                v-for="(dict, index) in ddTypeOptions"
                :key="index"
                :label="dict.dictValue"
                border
              >
                {{ $t(dict.dictLabel) }}
              </el-checkbox>
            </el-checkbox-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('尽调要求')" prop="ddAsk">
            <el-input v-model="form.ddAsk" :placeholder="$t('尽调要求')" type="textarea" show-word-limit :maxlength="2000" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('备注')" prop="remark">
            <el-input v-model="form.remark" type="textarea" show-word-limit :maxlength="2000" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 附件列表 -->
      <FileTable :list.sync="form.fileList" data-type="deal_ddApply" :business-id="getDealId" :btn-show="!otherBtnHide" :hidden-type-flag="true" />
    </el-form>
  </div>
</template>

<script>
    import { saveDealWfDdApply, getDealWfDdApply, getByWfNodeId } from '@/api/deal/form/dealWfDdApply'
    export default {
        data() {
            return {
                // 尽调类型
                ddTypeOptions: [],
                // 尽调方式
                ddModeOptions: [],
                // 项目id
                getDealId: this.$route.query.id,
                // 表单参数
                form: {
                  ddType: [],
                  fileList: []
                },
                // 表单校验
                rules: {
                  actTitle: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
                  ddType: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
                  ddMode: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }]
                },
                // 遮罩层
                loading: false,
                // 操作按钮默认不展示
                buttonShow: true,
                // 是否隐藏保存按钮
                otherBtnHide: false
            }
        },
        mounted() {
          this.$bus.$emit('async_done')
        },
      created() {
            this.getDicts('dd_type').then(response => {
              this.ddTypeOptions = response.data
              console.log(this.ddTypeOptions)
            })
            this.getDicts('dd_mode').then(response => {
              this.ddModeOptions = response.data
            })
        },
        methods: {
            // 初始化数据
            init(data) {
                this.loading = true
                if (data != null && data.id !== undefined) {
                    getDealWfDdApply(data.id).then(response => {
                        this.form = response.data
                        this.loading = false
                    })
                } else {
                  getByWfNodeId(data.wfNodeId).then(response => {
                      this.form = response.data
                      this.form.fileList = data.fileList
                      this.form.actTitle = data.actTitle
                      this.loading = false
                  })
                }
            },
            // 表单重置
            reset() {
                this.form = {
                    id: undefined,
                    dealId: this.getDealId ? this.getDealId : undefined,
                    companyId: undefined,
                    userId: undefined,
                    ddType: [],
                    ddMode: undefined,
                    ddAsk: undefined,
                    actTitle: undefined,
                    remark: undefined,
                    createBy: undefined,
                    createTime: undefined,
                    updateBy: undefined,
                    updateTime: undefined,
                    delFlag: undefined
                }
                this.resetForm('form')
            },
            /** 提交按钮 */
            submitForm: function() {
              return new Promise((resolve, reject) => {
                this.$refs['form'].validate(valid => {
                  // 验证数据
                  if (valid) {
                    // 弹出遮罩层
                    this.loading = true
                    // 保存数
                    this.form.fileList.forEach(item => {
                      item.directoryId = 'dea5f4d35c5b4c848d83ca0fcca313fd'
                    })
                    saveDealWfDdApply(this.form).then(response => {
                      if (response.code === 200) {
                        this.msgSuccess(this.$t('保存成功'))
                        this.loading = false
                        resolve(response.data.id)
                      } else {
                        this.msgError(response.msg)
                        this.loading = false
                        reject(response)
                      }
                    }).catch(() => {
                      this.loading = false
                    })
                  } else {
                    reject()
                    this.locationError(document)
                  }
                })
              })
           }
        }
    }
</script>
